package com.chase.framerwork.action;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.chase.framerwork.bean.Pager;
import com.chase.framerwork.common.Globle;
import com.chase.framerwork.entity.User;
import com.chase.framerwork.excel.Excel;
import com.chase.framerwork.exception.SessionLostException;
import com.chase.framerwork.message.Message;
import com.chase.framerwork.util.CookieUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action层最父类,所有Action都必须继承自该父类
 * 
 * @author Chase
 * 
 */
public abstract class BaseAction extends ActionSupport
{
	private static final long serialVersionUID = -5406156774433594044L;

	protected static final String LIST = "list";
	protected static final String VIEW = "view";

	protected static final String AJAX = "ajax";
	protected static final String AJAX_STATUS = "status";

	protected Long id; // id
	protected Long[] ids; // ids
	protected Pager pager;

	protected File upload;
	protected String uploadFileName;
	protected String uploadContentType;

	protected File[] uploads;
	protected Excel excel;

	protected String keywords;

	protected Message message;

	public BaseAction()
	{
		pager = new Pager();
	}

	// 是否是编辑页面
	public boolean isEdit()
	{
		if (id != null)
		{
			return true;
		}
		return false;
	}

	// 获取登录之后的用户对象
	public User getUser()
	{
		User user = (User) getSession(Globle.USER_SESSION);
		if (user == null)
		{
			throw new SessionLostException();
		}
		return user;
	}

	// 获取Request对象
	protected HttpServletRequest getRequest()
	{
		return ServletActionContext.getRequest();
	}

	// 获取Response对象
	protected HttpServletResponse getResponse()
	{
		return ServletActionContext.getResponse();
	}

	// 获取ServletContext对象
	protected ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}

	// 获取requestAttribute
	protected Object getAttribute(String name)
	{
		return ServletActionContext.getRequest().getAttribute(name);
	}

	// 设置Attribute
	protected void setAttribute(String name, Object value)
	{
		ServletActionContext.getRequest().setAttribute(name, value);
	}

	// 获取requestParameter
	protected String getParameter(String name)
	{
		return ServletActionContext.getRequest().getParameter(name);
	}

	// 获取requestParameter数组
	protected String[] getParameterValues(String name)
	{
		return ServletActionContext.getRequest().getParameterValues(name);
	}

	// 获取Session对象
	protected Object getSession()
	{
		return getRequest().getSession();
	}

	// 获取Session中的数据
	protected Object getSession(String name)
	{
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		return session.get(name);
	}

	// 设置Session
	protected void setSession(String name, Object value)
	{
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.put(name, value);
	}

	// 移除Session
	protected void removeSession(String name)
	{
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		session.remove(name);
	}

	// 获取Cookie
	protected String getCookie(String name)
	{
		return CookieUtil.getCookie(ServletActionContext.getRequest(), name);
	}

	// 设置Cookie
	protected void setCookie(String name, String value)
	{
		CookieUtil.addCookie(ServletActionContext.getRequest(), ServletActionContext.getResponse(), name, value,
		        ServletActionContext.getRequest().getContextPath() + "/");
	}

	// 设置Cookie
	protected void setCookie(String name, String value, Integer maxAge)
	{
		CookieUtil.addCookie(ServletActionContext.getRequest(), ServletActionContext.getResponse(), name, value,
		        ServletActionContext.getRequest().getContextPath() + "/", maxAge, null, null);
	}

	// 移除Cookie
	protected void removeCookie(String name)
	{
		CookieUtil.removeCookie(ServletActionContext.getRequest(), ServletActionContext.getResponse(), name,
		        ServletActionContext.getRequest().getContextPath() + "/");
	}

	// 获取真实路径
	protected String getRealPath(String path)
	{
		return ServletActionContext.getServletContext().getRealPath(path);
	}

	// 获取ContextPath
	protected String getContextPath()
	{
		return ServletActionContext.getRequest().getContextPath();
	}

	// AJAX输出
	protected void ajax(String content, String contentType)
	{
		try
		{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.getWriter().write(content);
			response.getWriter().flush();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	// 直接输出JSONObject
	protected void outJSON(JSONObject json) throws Exception
	{
		getResponse().setCharacterEncoding(Globle.CHARSET);
		getResponse().getWriter().write(json.toString());
	}

	// 输出成功JSON
	protected void outJSONSuccess(JSONObject json) throws Exception
	{
		json.put(AJAX_STATUS, SUCCESS);
		outJSON(json);
	}
	
	// 输出失败JSON
	protected void outJSONError(JSONObject json) throws Exception
	{
		json.put(AJAX_STATUS, ERROR);
		outJSON(json);
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public void setPager(Pager pager)
	{
		this.pager = pager;
	}

	public Pager getPager()
	{
		return pager;
	}

	public Long[] getIds()
	{
		return ids;
	}

	public void setIds(Long[] ids)
	{
		this.ids = ids;
	}

	public File getUpload()
	{
		return upload;
	}

	public void setUpload(File upload)
	{
		this.upload = upload;
	}

	public String getUploadFileName()
	{
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName)
	{
		this.uploadFileName = uploadFileName;
	}

	public File[] getUploads()
	{
		return uploads;
	}

	public void setUploads(File[] uploads)
	{
		this.uploads = uploads;
	}

	public Excel getExcel()
	{
		return excel;
	}

	public void setExcel(Excel excel)
	{
		this.excel = excel;
	}

	public String getKeywords()
	{
		return keywords;
	}

	public void setKeywords(String keywords)
	{
		this.keywords = keywords;
	}

	public String getUploadContentType()
	{
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType)
	{
		this.uploadContentType = uploadContentType;
	}

	public Message getMessage()
	{
		return message;
	}

	public void setMessage(Message message)
	{
		this.message = message;
	}

}
